﻿using MediatR;
using T.Basic.Domain.T.Basic.Dto;
using T.Basic.Domain.T.Basic.Model;
using T.Basic.Read.API.Appliction.Command.FuelCostCommand;
using T.Basic.Repository.BasicInterface;

namespace T.Basic.Read.API.Appliction.CommandHandlers.FuelCostHanders
{
    /// <summary>
    /// 显示油费信息
    /// </summary>
    public class FuelCostListCommandHander : IRequestHandler<FuelCostListCommand, PageDto<FuelCost>>
    {
        private readonly IFuelCostRepository _FuelCostrepository;
        public FuelCostListCommandHander(IFuelCostRepository fuelCostrepository)
        {
            _FuelCostrepository = fuelCostrepository;
        }
        public async Task<PageDto<FuelCost>> Handle(FuelCostListCommand request, CancellationToken cancellationToken)
        {
            PageDto<FuelCost> pageDto = new PageDto<FuelCost>();

            var FuelCostlist = await _FuelCostrepository.GetAsync(d => !d.IsDeleted);
            if (!string.IsNullOrEmpty(request.CompanyID))
            {
                FuelCostlist = FuelCostlist.Where(d => d.CompanyID == request.CompanyID).ToList();
            }
            if (!string.IsNullOrEmpty(request.LicensePlateNumber))
            {
                FuelCostlist = FuelCostlist.Where(d => d.LicensePlateNumber.Contains(request.LicensePlateNumber)).ToList();
            }
            if (!string.IsNullOrEmpty(request.HandledBy))
            {
                FuelCostlist= FuelCostlist.Where(d=>d.HandledBy==request.HandledBy).ToList();
            }
            if (request.PageIndex == null)
            {
                request.PageIndex = 1;
            }
            if (request.PageSize == null)
            {
                request.PageSize = FuelCostlist.Count();
            }
            var size = request.PageSize;
            var index = request.PageIndex;
            pageDto.TotalCount = FuelCostlist.Count();
            pageDto.PageCount = (int)Math.Ceiling(pageDto.PageCount * 1.0 / (int)size);
            pageDto.list = FuelCostlist.OrderByDescending(d => d.CreatedDate).Skip(((int)index - 1) * (int)size).Take((int)size).ToList();
            return pageDto;
            //pageDto.TotalCount = FuelCostlist.Count();
            //pageDto.PageCount = (int)Math.Ceiling(pageDto.TotalCount * 1.0 / request.PageSize);
            //pageDto.list = FuelCostlist.OrderByDescending(d => d.CreatedDate).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            //return pageDto;

        }
    }
}
